package cn.snmp.mutual.nms;

class ASNTypes
{
    public static int MAX_SUBID;
    public static int MAX_OID_LEN;
    public static final byte ASN_BOOLEAN = 1;
    public static final byte ASN_INTEGER = 2;
    public static final byte ASN_BIT_STR = 3;
    public static final byte ASN_OCTET_STR = 4;
    public static final byte ASN_NULL = 5;
    public static final byte ASN_OBJECT_ID = 6;
    public static final byte ASN_SEQUENCE = 16;
    public static final byte ASN_SET = 17;
    public static final byte ASN_UNIVERSAL = 0;
    public static final byte ASN_APPLICATION = 64;
    public static final byte ASN_CONTEXT = Byte.MIN_VALUE;
    public static final byte ASN_PRIVATE = -64;
    public static final int ASN_EXCEPTION = 128;
    public static final byte ASN_PRIMITIVE = 0;
    public static final byte ASN_CONSTRUCTOR = 32;
    public static final byte ASN_LONG_LEN = Byte.MIN_VALUE;
    public static final byte ASN_EXTENSION_ID = 31;
    public static final byte ASN_BIT8 = Byte.MIN_VALUE;
    
    public static boolean IS_CONSTRUCTOR(final byte byte0) {
        return (byte0 & 0x20) != 0x0;
    }
    
    public static boolean IS_EXTENSION_ID(final byte byte0) {
        return (byte0 & 0x1F) == 0x1F;
    }
    
    public static int byteToInt(final byte byte0) {
        return byte0 & 0xFF;
    }
    
    public static int encodeInt(final byte[] abyte0, int i, int j, final int k) throws ArrayIndexOutOfBoundsException {
        final byte byte0 = (byte)((j >= 0) ? 0 : -128);
        int l = 0;
        while (true) {
            abyte0[--i] = (byte)(j & 0xFF);
            ++l;
            if ((j & 0xFFFFFF80) == byte0) {
                break;
            }
            j >>= 8;
        }
        abyte0[--i] = (byte)l;
        abyte0[--i] = (byte)k;
        return i;
    }
    
    private static int encodeLength(final byte[] abyte0, int i, int j) throws ArrayIndexOutOfBoundsException {
        if (j < 128) {
            abyte0[--i] = (byte)j;
        }
        else {
            int k = 0;
            do {
                abyte0[--i] = (byte)(j & 0xFF);
                j >>>= 8;
                ++k;
            } while (j != 0);
            abyte0[--i] = (byte)(k | 0x80);
        }
        return i;
    }
    
    public static int encodeNull(final byte[] abyte0, int i, final int j) throws ArrayIndexOutOfBoundsException {
        abyte0[--i] = 0;
        abyte0[--i] = (byte)j;
        return i;
    }
    
    public static int encodeOctets(final byte[] abyte0, int i, final byte[] abyte1, final int j, int k, final int l) throws ArrayIndexOutOfBoundsException {
        if (abyte1 != null) {
            System.arraycopy(abyte1, j, abyte0, i - k, k);
        }
        else {
            k = 0;
        }
        i = encodeLength(abyte0, i - k, k);
        abyte0[--i] = (byte)l;
        return i;
    }
    
    public static int encodeOid(final byte[] abyte0, int i, final int[] ai, final int j, final int k) throws ArrayIndexOutOfBoundsException {
        int l = 1;
        if (j < 2) {
            abyte0[--i] = 0;
        }
        else {
            for (int i2 = j - 1; i2 > 1; --i2) {
                int j2 = ai[i2];
                abyte0[--i] = (byte)(j2 & 0x7F);
                ++l;
                while ((j2 & 0xFFFFFF80) != 0x0) {
                    j2 >>>= 7;
                    abyte0[--i] = (byte)((j2 & 0x7F) | 0x80);
                    ++l;
                }
            }
            abyte0[--i] = (byte)(40 * ai[0] + ai[1]);
        }
        i = encodeLength(abyte0, i, l);
        abyte0[--i] = (byte)k;
        return i;
    }
    
    public static int encodeSequence(final byte[] abyte0, int i, final int j, final int k) throws ArrayIndexOutOfBoundsException {
        i = encodeLength(abyte0, i, j);
        abyte0[--i] = (byte)k;
        return i;
    }
    
    public static int encodeString(final byte[] bytes, int i, final String s, final int j, int k, final int l) throws ArrayIndexOutOfBoundsException {
        if (s != null) {
            s.getBytes(j, j + k, bytes, i - k);
        }
        else {
            k = 0;
        }
        i = encodeLength(bytes, i - k, k);
        bytes[--i] = (byte)l;
        return i;
    }
    
    public static int encodeUInt(final byte[] bytes, int i, int j, final int k) throws ArrayIndexOutOfBoundsException {
        int l = 0;
        while (true) {
            bytes[--i] = (byte)(j & 0xFF);
            ++l;
            if ((j & 0xFFFFFF80) == 0x0) {
                break;
            }
            j >>>= 8;
        }
        bytes[--i] = (byte)l;
        bytes[--i] = (byte)k;
        return i;
    }
    
    public static int encodeUInt(final byte[] bytes, int i, final long[] al, final int j) throws ArrayIndexOutOfBoundsException {
        int k = 0;
        long l = 0L;
        l |= (al[1] & -1L);
        l <<= 32;
        l |= (al[0] & -1L);
        do {
            bytes[--i] = (byte)(l & 0xFFL);
            ++k;
            l >>>= 8;
        } while (l != 0L);
        if ((bytes[i] & 0xFF) > 127) {
            bytes[--i] = 0;
            ++k;
        }
        bytes[--i] = (byte)k;
        bytes[--i] = (byte)j;
        return i;
    }
    
    public static byte[] encodeVariable(final SnmpVar var) {
        int i = 10;
        int j = 0;
        byte[] bytes = new byte[i];
        while (true) {
            try {
                j = var.encode(bytes, bytes.length);
            }
            catch (ArrayIndexOutOfBoundsException _ex) {
                i += i;
                bytes = new byte[i];
                continue;
            }
            break;
        }
        final byte[] abyte1 = new byte[i - j];
        System.arraycopy(bytes, j, abyte1, 0, abyte1.length);
        return abyte1;
    }
    
    static {
        ASNTypes.MAX_SUBID = -1;
        ASNTypes.MAX_OID_LEN = 64;
    }
}
